Repository navigation
LWG-4490 Allow std ranges size in std ranges count - #6508
Open
Syed Maroof Hussain (smhussa22) wants to merge 2 commits into
Open
Syed Maroof Hussain (smhussa22) wants to merge 2 commits into
Syed Maroof Hussain (smhussa22) wants to merge 2 commits into
Conversation
…ccess ranges via LWG-4490
Syed Maroof Hussain (smhussa22)
requested a review
from a team
as a code owner
November 7, 2026 03:28
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Author
|
@microsoft-github-policy-service agree |
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues for the change enabling vectorized std::ranges::count on eligible sized ranges with unsized sentinels.
No open issues 🎉
What changed in this PR
Implements part of LWG-4490 (#6320) for std::ranges::count, allowing sized ranges with random-access iterators and unsized sentinels to use the existing vectorized path.
Changes:
- Calculates the end iterator from the range’s size when eligible, preserving the existing path otherwise.
- Adds compile-time and runtime tests for correct counts and avoidance of sentinel comparisons.
- Adds a benchmark comparing an eligible unsized-sentinel range with a plain vector.
| File | Description |
|---|---|
| tests/std/tests/P0896R4_ranges_alg_count/test.cpp | Tests sized ranges with unsized sentinels, including projections and varying lengths. |
| stl/inc/algorithm | Computes an iterator endpoint from the size for eligible ranges::count calls. |
| benchmarks/src/ranges_count_sized_range_unsized_sentinel.cpp | Compares vector and unsized-sentinel range counting performance. |
| benchmarks/CMakeLists.txt | Registers the new benchmark target. |
Review effort: Balanced
Member
|
/azp run STL-CI |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Disclosure
Extension of/implements part of #6320
Intention of this PR
The wording changes in LWG-4490 allow for range overloads of algorithms to calculate the end of a range as begin + size, instead of a full walk to it.
This PR intends to take advantage of this only for
std::ranges::count, and is related to #6477, which has attempted this for some other ranges algorithms.PR summary/explanation
The implementation for
std::ranges::countalready has a fast vectorized path, but it only runs when the end of a range can be subtracted from the beginning.Ranges that know their size, but whose end is not a sized sentinel, could only use the non-vectorized version which would simply do a walk of the entire range one element at a time.
However, LWG-4490 changed the wording such that now the end can be calculated as begin + size.
Thus, this PR changes
std::ranges::countto calculate the end as begin + size when a range:If any of the three conditions above are false, this change doesn't apply and the range behaves as it did before. Ranges who's end is already a sized sentinel keep using the vectorized path.
Benchmarks and tests
This PR has added a benchmark that compares the speeds of
std::ranges::counton two ranges holding the exact same data:Note that range 2 would have to use the non-vectorized path prior to the changes, but now qualifies for the vectorized path after the changes.
I ran this benchmark five times for both before and after the change, and the following table shows the medians of each of those runs:
vectorbeforevectorafterNoticeably, range 2 runs at about the same speed as a plain
vectorand has experienced a speedup overall.vectorhasn't experienced any change (apart from usual variability), which is to be expected.This PR also extends an existing test for
std::ranges::count, with a test range that knows its size but who's end is not a sized sentinel, and checks:std::ranges::countis still correctThis test fails without the changes made in this PR, because the end is compared while walking the range. It passes with the changesmade in this PR.